//
//  RankViewController.m
//  BallRankingProject
//
//  Created by lanou3g on 15/5/29.
//  Copyright (c) 2015年 luojian. All rights reserved.
//

#import "RankViewController.h"
#import "UIImageView+WebCache.h"
@interface RankViewController ()

@end

@implementation RankViewController
//- (void)loadView{
//    //test
//    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0,0,CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds)-64-44)];
//    self.view = view;
//    //
//
//}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
   self.navigationController.navigationBar.translucent = NO;
    CGRect fram=self.view.frame;
    fram.origin.x=CGRectGetWidth(self.view.frame);
    fram.size.height=CGRectGetHeight(self.view.frame)-44;
    self.view.frame=fram;
       self.allArray=[NSMutableArray array];
     self.titleArray=[NSMutableArray array];
   // self.view.backgroundColor=[UIColor clearColor];
    NSArray *array=[NSArray arrayWithObjects:@"积分榜",@"射手榜",@"助攻榜", nil];
    self.segmentVC=[[UISegmentedControl alloc] initWithItems:array];
    CGRect frame = [UIScreen mainScreen].bounds;
    
   
    _segmentVC.frame=CGRectMake(0,0,frame.size.width, 40);

    //去掉颜色
    self.segmentVC.tintColor =[UIColor clearColor];
    //设置选中字体的颜色
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],NSForegroundColorAttributeName: [UIColor whiteColor]};
    [self.segmentVC setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    //设置未选中的字体颜色
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName: [UIColor colorWithRed:72/255.0 green:105/255.0 blue:141/255.0 alpha:1.0]};
    [self.segmentVC setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
     self.segmentVC.backgroundColor=[UIColor colorWithRed:26/255.0 green:52/255.0 blue:78/255.0 alpha:1.000];
    
     [_segmentVC addTarget:self action:@selector(exchangeView:) forControlEvents:(UIControlEventValueChanged)];
    // _segmentVC.backgroundColor=[UIColor colorWithRed:0.276 green:0.419 blue:0.569 alpha:1.000];
    _segmentVC.selectedSegmentIndex=0;
    
    [self.view addSubview:_segmentVC];
    

    
    self.scoreView=[[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_segmentVC.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-104)];
    
    self.shooterView=[[UITableView alloc] initWithFrame:CGRectMake(0,CGRectGetHeight(_segmentVC.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-104)];
    _scoreView.delegate=self;
    _scoreView.dataSource=self;
    _shooterView.delegate=self;
    _shooterView.dataSource=self;
    
   // [self.view addSubview:_scoreView];
   // [self.view addSubview:_shooterView];
    self.assistView=[[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_segmentVC.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-104)];
    _assistView.delegate=self;
    _assistView.dataSource=self;
    //_assistView.backgroundColor=[UIColor colorWithRed:1.000 green:0.349 blue:0.092 alpha:1.000];
    [self getinformage];
    //self.segmentVC.tintColor=[UIColor whiteColor];
    
   
}
//- (void)viewWillAppear:(BOOL)animated{
//    
//    NSLog(@"%@,rankView",self.view);
//    CGRect frame = self.view.frame;
//    frame.origin.y = 0;
//    self.view.frame =frame;
//}
- (void)exchangeView:(UISegmentedControl *)segment
{
    switch (segment.selectedSegmentIndex) {
        case 0:
         
                if (_scoreView.superview==nil) {
                    [_shooterView removeFromSuperview];
                    [_assistView removeFromSuperview];
                    [self getinformage];
                    [_scoreView reloadData];
                    [self.view addSubview:_scoreView];
                }
                
                
           
            break;
        case 1:
        
            if (_shooterView.superview==nil) {
                [_scoreView removeFromSuperview];
                [self getinformage];
                [_assistView removeFromSuperview];
                [_shooterView reloadData];
                [self.view addSubview:_shooterView];
               // _shooterView.backgroundColor=[UIColor colorWithRed:0.437 green:0.461 blue:1.000 alpha:1.000];
                // [_shooterView reloadData];
            }
            break;
        case 2:
       
            if (_assistView.superview==nil) {
                [_scoreView removeFromSuperview];
                [_shooterView removeFromSuperview];
                [self getinformage];
                [_assistView reloadData];
              // _assistView.backgroundColor=[UIColor colorWithRed:1.000 green:0.879 blue:0.124 alpha:1.000];
                [self.view addSubview:_assistView];
            }
            break;
        default:
            break;
    }
    
    
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//    return 100;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @" ";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_segmentVC.selectedSegmentIndex==0) {
    ScoreViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"myCell"];
    //if (!cell) {
    cell=[[ScoreViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"myCell"];
//}
    NSMutableArray *array1=_array[indexPath.row];
    cell.lable1.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:0]];
        cell.lable1.textAlignment=1;
        
        if ([[array1 objectAtIndex:0] intValue]<4) {
            
            cell.lable1.backgroundColor=[UIColor colorWithRed:1.000 green:0.134 blue:0.054 alpha:1.000];
            cell.lable1.layer.cornerRadius = cell.lable1.bounds.size.width/2;
            cell.lable1.layer.masksToBounds = YES;
            
           // cell.lable1.layer.cornerRadius=50;
          //  cell.lable1.layer.shadowColor=[UIColor redColor].CGColor;
        }
        
    cell.lable7.text=[array1 objectAtIndex:7];
    cell.lable2.text=[array1 objectAtIndex:1];
         NSString *str=[array1 objectAtIndex:2];
        [cell.imageViews sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"placeHold"]];

    cell.lable3.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:3]];
    cell.lable4.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:4]];
    cell.lable5.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:5]];
    cell.lable6.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:6]];
    cell.lable8.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:8]];
    return cell;
    }
    else
        if (_segmentVC.selectedSegmentIndex==1) {
            ShooterCell *cell=[tableView dequeueReusableCellWithIdentifier:@"shootCell"];
           

            if (!cell) {
                cell=[[ShooterCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"shootCell"];
            }
            NSMutableArray *array1=_array[indexPath.row];
            
            cell.numberLable1.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:0]];
            
            cell.numberLable1.backgroundColor=[UIColor colorWithRed:0.517 green:0.558 blue:0.910 alpha:1.000];
            cell.numberLable1.layer.cornerRadius = cell.numberLable1.bounds.size.width/2;
            cell.numberLable1.layer.masksToBounds = YES;
            
            cell.nameLable2.text=array1[1];
            NSString *str=[array1 objectAtIndex:2];
            [cell.imagesView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"placeHold"]];
            cell.teamLable3.text=array1[3];
            cell.ballLable4.text=array1[4];
            
           
            return cell;
        }
    else{
        
        ShooterCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
        
        if (!cell) {
            cell=[[ShooterCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
        }
        NSMutableArray *array1=_array[indexPath.row];
           
        
        cell.numberLable1.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:0]];
        cell.numberLable1.backgroundColor=[UIColor colorWithRed:0.517 green:0.558 blue:0.910 alpha:1.000];
        cell.numberLable1.layer.cornerRadius = cell.numberLable1.bounds.size.width/2;
        cell.numberLable1.layer.masksToBounds = YES;
        cell.nameLable2.text=array1[1];
        NSString *str=[array1 objectAtIndex:2];
        [cell.imagesView sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"placeHold"]];
        
        cell.teamLable3.text=array1[3];
        cell.ballLable4.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:4]];;
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
- (void)getinformage
{
   NSString *string=[NSString stringWithFormat:MICM];
    NSURL *url=[NSURL URLWithString:string];
    NSData *data=[NSData dataWithContentsOfURL:url];
    if (data==nil) {
        
        return ;
    }
    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
   _allArray=[dic objectForKey:@"result"];
  //  NSLog(@"%@",_allArray);
    if (_segmentVC.selectedSegmentIndex==0) {
        
      //  _allArray=_scoreView.array;
       // _allArray=_scoreView.array;
        //   NSLog(@"--+++%@",_allArray);
        [self.view addSubview:_scoreView];
        NSDictionary *dic=_allArray[0];
       NSArray *arr=[dic objectForKey:@"ranks_data"];
        // NSLog(@"+-+-%@",_array);
        _titleArray=[arr objectAtIndex:0];
        _array=nil;
        
        if (_array==nil) {
            self.array=[NSMutableArray array];
        for (int i=1;i<arr.count ; i++) {
            NSArray *arr2=arr[i];
            [_array addObject:arr2];
        }
        }
    }
  else  if (_segmentVC.selectedSegmentIndex==1) {
      
      
           _array=nil;
      
        [self.view addSubview:_shooterView];
        NSDictionary *dic=_allArray[1];
        NSArray *arr=[dic objectForKey:@"ranks_data"];
        _titleArray=[arr objectAtIndex:0];
        if (_array==nil) {
            self.array=[NSMutableArray array];
            for (int i=1;i<arr.count ; i++) {
                NSArray *arr2=arr[i];
                [_array addObject:arr2];
            }
        }
 
  }else  if (_segmentVC.selectedSegmentIndex==2) {
      _array=nil;
      
      [self.view addSubview:_assistView];
      NSDictionary *dic=_allArray[2];
      NSArray *arr=[dic objectForKey:@"ranks_data"];
      _titleArray=arr[0];
     
      if (_array==nil) {
          self.array=[NSMutableArray array];
          for (int i=1;i<arr.count ; i++) {
              NSArray *arr2=arr[i];
              [_array addObject:arr2];
          }
      }
      
  }

    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  
    CGRect frame=[UIScreen mainScreen].bounds;
    frame.size.height=30;
    UIView *headView=[[UIView alloc] initWithFrame:frame];
    
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0,CGRectGetWidth(headView.frame),1)];
    view.backgroundColor=[UIColor colorWithRed:0.159 green:0.237 blue:0.545 alpha:1.000];
    [headView addSubview:view];
    
    
     headView.backgroundColor=[UIColor colorWithRed:0.276 green:0.419 blue:0.569 alpha:1.000];
     if (_segmentVC.selectedSegmentIndex==0) {
UILabel *lable1=[[UILabel alloc] initWithFrame:CGRectMake(5, 5, CGRectGetWidth(headView.frame)/10, CGRectGetHeight(headView.frame)-10)];
UILabel *lable2=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*2, 5, CGRectGetWidth(tableView.frame)/10*2, CGRectGetHeight(headView.frame)-10)];
UILabel *lable3=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*4, 5, CGRectGetWidth(headView.frame)/10, CGRectGetHeight(headView.frame)-10)];
UILabel *lable4=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*5, 5, CGRectGetWidth(headView.frame)/10, CGRectGetHeight(headView.frame)-10)];
UILabel *lable5=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*6, 5, CGRectGetWidth(headView.frame)/10, CGRectGetHeight(headView.frame)-10)];
UILabel *lable6=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*7, 5, CGRectGetWidth(headView.frame)/10, CGRectGetHeight(headView.frame)-10)];
UILabel *lable7=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*8-5, 5, CGRectGetWidth(headView.frame)/10+5, CGRectGetHeight(headView.frame)-10)];
UILabel *lable8=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(frame)/10*9, 5, CGRectGetWidth(headView.frame)/10, CGRectGetHeight(headView.frame)-10)];
    
    [headView addSubview:lable1];
    [headView addSubview:lable2];
    [headView addSubview:lable3];
    [headView addSubview:lable4];
    [headView addSubview:lable5];
    [headView addSubview:lable6];
    [headView addSubview:lable7];
    [headView addSubview:lable8];
    lable1.text=_titleArray[0];
    lable2.text=_titleArray[1];
     lable3.text=_titleArray[3];
     lable4.text=_titleArray[4];
     lable5.text=_titleArray[5];
     lable6.text=_titleArray[6];
     lable7.text=_titleArray[7];
     lable8.text=_titleArray[8];
    lable1.font=[UIFont systemFontOfSize:10.0];
    lable2.font=[UIFont systemFontOfSize:10.0];
    lable3.font=[UIFont systemFontOfSize:10.0];
    lable4.font=[UIFont systemFontOfSize:10.0];
    lable5.font=[UIFont systemFontOfSize:10.0];
    lable6.font=[UIFont systemFontOfSize:10.0];
    lable7.font=[UIFont systemFontOfSize:10.0];
    lable8.font=[UIFont systemFontOfSize:10.0];
    }
    if (_segmentVC.selectedSegmentIndex==1) {
        
        UILabel *numberLable=[[UILabel alloc] initWithFrame:CGRectMake(5, 5, CGRectGetWidth(headView.frame)/6, CGRectGetHeight(headView.frame)-10)];
        UILabel *nameLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6*2, 5, CGRectGetWidth(headView.frame)/6*2, CGRectGetHeight(headView.frame)-10)];
        UILabel *teamLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6*4, 5, CGRectGetWidth(headView.frame)/6, CGRectGetHeight(headView.frame)-10)];
        UILabel *ballLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6*5, 5, CGRectGetWidth(headView.frame)/6+10, CGRectGetHeight(headView.frame)-10)];
        [headView addSubview:numberLable];
        [headView addSubview:nameLable];
        [headView addSubview:teamLable];
        [headView addSubview:ballLable];
        numberLable.text=[NSString stringWithFormat:@"%@",_titleArray[0]];
        numberLable.textAlignment=1;
        nameLable.text=_titleArray[1];
        teamLable.text=_titleArray[3];
        ballLable.text=_titleArray[4];
        ballLable.font=[UIFont systemFontOfSize:13.0];
        numberLable.font=[UIFont systemFontOfSize:13.0];
        nameLable.font=[UIFont systemFontOfSize:13.0];
        teamLable.font=[UIFont systemFontOfSize:13.0];
        
        //headView.backgroundColor=[UIColor colorWithRed:1.000 green:0.110 blue:0.240 alpha:1.000];
    }else
        if (_segmentVC.selectedSegmentIndex==2)
    {
        
       // headView.backgroundColor=[UIColor colorWithRed:1.000 green:0.103 blue:0.144 alpha:1.000];
        UILabel *numberLable=[[UILabel alloc] initWithFrame:CGRectMake(5, 5, CGRectGetWidth(headView.frame)/6, CGRectGetHeight(headView.frame)-10)];
        UILabel *nameLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6, 5, CGRectGetWidth(headView.frame)/6*3, CGRectGetHeight(headView.frame)-10)];
        UILabel *teamLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6*4, 5, CGRectGetWidth(headView.frame)/6, CGRectGetHeight(headView.frame)-10)];
        UILabel *ballLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6*5, 5, CGRectGetWidth(headView.frame)/6+10, CGRectGetHeight(headView.frame)-10)];
        [headView addSubview:numberLable];
        [headView addSubview:nameLable];
        [headView addSubview:teamLable];
        [headView addSubview:ballLable];
        numberLable.text=[NSString stringWithFormat:@"%@",_titleArray[0]];
        numberLable.textAlignment=1;
        nameLable.textAlignment=1;
        teamLable.textAlignment=1;
        //ballLable.textAlignment=1;
//        nameLable.backgroundColor=[UIColor redColor];
//        numberLable.backgroundColor=[UIColor blackColor];
//        teamLable.backgroundColor=[UIColor yellowColor];
//        ballLable.backgroundColor=[UIColor blueColor];
        nameLable.text=_titleArray[1];
        teamLable.text=_titleArray[3];
        ballLable.text=_titleArray[4];
       // ballLable.font=[UIFont systemFontOfSize:13.5];
        ballLable.font=[UIFont systemFontOfSize:13.0];
        numberLable.font=[UIFont systemFontOfSize:13.0];
        nameLable.font=[UIFont systemFontOfSize:13.0];
        teamLable.font=[UIFont systemFontOfSize:13.0];
        
    
    
    }
   
        return headView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_segmentVC.selectedSegmentIndex==0) {

        
            ScoreViewController *scoreVC=[[ScoreViewController alloc] init];
            NSArray *arr= _array[indexPath.row];
            scoreVC.title=arr[1];
        
         scoreVC.string=arr[9];
        /*
        CATransition *transition = [CATransition animation];
        
        transition.duration = 1.0f;
        
        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
        
        transition.type = kCATransitionPush;
        
        transition.subtype = kCATransitionFromTop;
        
        transition.delegate = self;
        
        [scoreVC.view.layer addAnimation:transition forKey:nil];
//        UINavigationController *NC=[[UINavigationController alloc] initWithRootViewController:scoreVC];
       // [self.view addSubview:scoreVC.view];
       // NSLog(@"%@",self.navigationController);//test
   // [self presentViewController:NC animated:YES completion:nil];
         */
        [self.navigationController pushViewController:scoreVC animated:YES];
        
        
    }else
    {
    BallViewController *ballVC=[[BallViewController alloc] init];
   
   NSArray *arr= _array[indexPath.row];
    
    ballVC.title=arr[1];
    ballVC.string=arr[5];
        
//        CATransition *transition = [CATransition animation];
//        
//        transition.duration = 1.0f;
//        
//        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//        
//        transition.type = kCATransitionPush;
//        
//        transition.subtype = kCATransitionFromRight;
//        
//        transition.delegate = self;
//        self.navigationController.navigationBar.translucent = NO;
//        [ballVC.view.layer addAnimation:transition forKey:nil];
        
    [self.navigationController pushViewController:ballVC animated:YES];
    }

}
@end
